<template>
  <div>
    <div class="container" style="height: 100vh">
      <div class="login-wrapper">
        <div class="header">用户注册</div>
        <div class="form-wrapper">
          <input
            v-model="rname"
            type="text"
            name="username"
            placeholder="请输入用户名"
            class="input-item"
            @blur="checkName"
          />
          <span :style="{ visibility: isVisibility1 }">用户名格式不正确,必须为汉字</span>
          <input
            v-model="rpwd"
            type="password"
            name="password"
            placeholder="请输入密码"
            class="input-item"
            @blur="checkPwd"
          />
            <span :style="{ visibility: isVisibility2 }">密码必须为6位数字</span>
          <input
            v-model="rpwd1"
            type="password"
            name="password"
            placeholder="请再次输入密码"
            class="input-item"
            @blur="checkRe_pwd"
          />
          <span :style="{ visibility: isVisibility3 }">两次密码不一致</span>
          <!-- <input @click="register" class="btn" type="button" value="注册" /> -->
          <template>
                  <div :plain="true" @click="register" class="btn">注册</div>
          </template>
        </div>
        <div class="msg">
          已有账户?
          <router-link to="/login">登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rname: "",
      rpwd: "",
      rpwd1: "",
      isVisibility1: "hidden",
      isVisibility2: "hidden",
      isVisibility3: "hidden",
    };
  },
  methods: {
    register() {
      if(this.checkName() && this.checkPwd() && this.checkRe_pwd()){
      // console.log('验证成功,执行注册业务...');
      const url = "user/register";
      const params = `rname=${this.rname}&rpwd=${this.rpwd}`;
      this.axios.post(url, params).then((res) => {
        console.log("注册页面数据:", res);
        if (res.data.code == 200) {
          // alert('恭喜您,注册成功')
          this.$message({
          message: '恭喜您，注册成功',
          type: 'success'
        });
          this.$router.push("/login");
        } else {
          this.$message.error('很抱歉,注册失败');
        }
      });
      }
    },
    checkName(){
        let exp=/^[\u4e00-\u9fa5]{0,}$/;
        if (exp.test(this.rname)) {
        this.isVisibility1 = "hidden";
        return true;
      } else {
        this.isVisibility1 = "visible";
        return false;
      }
    },
    checkPwd() {
      let exp=/^\d{6}$/;
      if (exp.test(this.rpwd)) {
        this.isVisibility2 = "hidden";
        return true;
      } else {
        this.isVisibility2 = "visible";
        return false;
      }
    },
     checkRe_pwd(){
        let exp=/^\d{6}$/;
        if(exp.test(this.rpwd) && this.rpwd == this.rpwd1){
            this.isVisibility3 = "hidden";
            return true
        }else{
            this.isVisibility3 = "visible";
            return false
        }
    }
  },
};
</script>
<style scoped>
.container {
  height: 100%;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}
.login-wrapper {
  background-color: #fff;
  width: 358px;
  height: 600px;
  border-radius: 15px;
  padding: 0 50px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}
.input-item {
  display: block;
  width: 100%;
  margin-bottom: 0px;
  border-style: none;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}
.input-item:placeholder {
  text-transform: uppercase;
}
span{
    font-size: 8px;
    display: block;
    margin: 0 auto;
    color: #e4464b;
    visibility: hidden;
}
.btn {
  text-align: center;
  padding: 10px;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  /* background-color: linear-gradient(to right, #a6c1ee, #fbc2eb); */
  color: #fff;
  user-select: none;
  border: none;
}
.msg {
  text-align: center;
  line-height: 88px;
}
a {
  text-decoration-line: none;
  color: #abc1ee;
}
</style>
<style lang="scss" scoped></style>
